package de.gameduell.framework.chat {
	import de.gameduell.framework.application.controller.IMultiplayerController;
	import de.gameduell.framework.application.protocol.MultiplayerProtocol;
	import de.gameduell.framework.gui.button.Button;
	import de.gameduell.framework.gui.button.ButtonEvent;
	import de.gameduell.framework.gui.button.ToggleButton;
	import de.gameduell.framework.gui.popup.Popup;
	import de.gameduell.framework.gui.popup.PopupEvent;
	import de.gameduell.net.src.communication.ServerMessageEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.ui.Keyboard;	

	/**
	 * @author cbra
	 */
	public class ChatControls {		

		protected var content:Sprite;
		protected var _openChatButton:Button;
		protected var sendChatButton:Button;
		protected var chatActiveButton:ToggleButton;
		protected var chatMessage:TextField;
		protected var controller:IMultiplayerController;
		protected var hasInputChat:Boolean;
		protected var _active:Boolean;
		protected var _chatPopup:Popup;

		public function ChatControls(aContent:Sprite,aController:IMultiplayerController, aChatPopup:Popup, activateSprite:Sprite, openChatSprite:Sprite, sendChatSprite:Sprite, chatMessageField:TextField = null) {
			content = aContent;
			controller = aController;
			controller.addEventListener(MultiplayerProtocol.CHAT, onChatMessage);
			content.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			
			_openChatButton = new Button(openChatSprite);
			_openChatButton.addEventListener(ButtonEvent.CLICK, openChat);
			sendChatButton = new Button(sendChatSprite);
			sendChatButton.addEventListener(ButtonEvent.CLICK, sendChat);
			
			if(activateSprite != null){
				chatActiveButton = new ToggleButton(activateSprite);
				chatActiveButton.active = true;
				chatActiveButton.toolTip = true;
				chatActiveButton.addEventListener(MouseEvent.CLICK, toggleChatActive);
			}
			
			_chatPopup = aChatPopup;
			_chatPopup.addEventListener(PopupEvent.HIDE, onChatPopupHide);
			
			hasInputChat = chatMessageField != null;
			if (hasInputChat) {
				chatMessage = chatMessageField;
				chatMessage.visible = false;
			}
			_active = false;
		}

		protected function keyDown(event:KeyboardEvent):void {				
			switch(event.keyCode) {
				case Keyboard.ENTER:
					if(_active) {
						sendChat();
					}
					break;
			}
		}

		public function get openChatButton():Button {
			return _openChatButton;
		}

		public function get chatPopup():Popup {
			return _chatPopup;
		}

		public function get active():Boolean {
			return _active;
		}

		protected function onChatMessage(event:ServerMessageEvent):void {
			event.parameters["playerId"];
			event.parameters["message"];
		}

		protected function openChat(event:ButtonEvent = null):void {
			_chatPopup.show();
			if (hasInputChat) {
				chatMessage.text = "";
				chatMessage.visible = true;
				content.stage.focus = chatMessage;
			}
			_openChatButton.visible = false;
			_active = true;
		}

		protected function sendChat(event:ButtonEvent = null):void {
			if (hasInputChat) {
				if(chatMessage.text != "") {
					controller.sendChatMessage(chatMessage.text);
				}
			}
			_chatPopup.hide();	
			_active = false;
		}

		private function onChatPopupHide(event:Event):void {
			if(_active && controller.chatActive) {	
				_openChatButton.visible = true;
			}
		}

		private function toggleChatActive(event:Event):void {
			controller.chatActive = !controller.chatActive;
			_openChatButton.visible = _chatPopup.visible? false :controller.chatActive;
			if (!controller.chatActive) {
				_chatPopup.hide();
				_active = false;
			}
		}
	}
}
